package formal.tree.recursion;

/**
 * 路径总和
 *
 * @author DengYuan2
 * @create 2021-01-12 21:29
 */
public class E_112 {
    public static void main(String[] args) {
        Integer[] a = {5, 4, 8, 11, null, 13, 4, 7, 2, null, null, null, 1};
        TreeNode treeNode = TreeNode.generateTree(a);
        boolean b = hasPathSum(treeNode, 26);
        System.out.println(b);
    }

    public static boolean hasPathSum(TreeNode root, int sum) {
        if (root == null) {
            return false;
        }
        sum = sum - root.val;
        if (sum == 0 && root.left == null && root.right == null) {
            return true;
        }
        return hasPathSum(root.left, sum) || hasPathSum(root.right, sum);
    }
}
